
//获取应用实例  
var app = getApp();
var that;
var common = require("../../utils/common.js");
Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0,

    pages: [1, 1, 1, 1, 1],  //请求的页面数
    orders:[],
    orderList1: [],
    orderList2: [],
    orderList3: [],
    shuaxin: true,

    re_jia:0,  //是否重新加载
  },
  onLoad: function (options) {
    that = this
    if (options.cur) { that.setData({ currentTab: parseInt(options.cur) }) }
    that.initSystemInfo();
      // that.get_orders()  每次进入都会调用bindChange房法，故不需要在此地调用
  },
  onShow: function () {
    if (that.data.re_jia == 1) {
    that.data.pages[that.data.currentTab] = 1;
    that.setData({ orders:[],orderList1: [], orderList2: [], orderList3: [] });
     that.get_orders()
    }
  },
  onHide: function () {
    that.setData({ re_jia: 1 });
  },
  initSystemInfo: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },

  bindChange: function (e) {
    that.setData({ currentTab: e.detail.current });
    that.setData({ re_jia: 0 });
    that.get_orders();
  },
  swichNav: function (e) {
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({
        currentTab: parseInt(current),
        isStatus: e.target.dataset.otype,
      });
    };
  },
  server_m: function (e) {//跳到猫咪下单 的详情
    var id = e.currentTarget.dataset.id
    var s = [that.data.orders, that.data.orderList1, that.data.orderList2, that.data.orderList3]
    var t = s[that.data.currentTab]
    wx.navigateTo({
      url: '../my_order/ser_order?ordersn=' +t[id].order.ordersn,
    })
  },
  house_m: function (e) {//跳到预约房间的详情
    var id = e.currentTarget.dataset.id
    var s = [that.data.orders, that.data.orderList1, that.data.orderList2, that.data.orderList3]
    var t = s[that.data.currentTab]
    wx.navigateTo({
      url: '../advance/advance?ordersn=' + t[id].order.ordersn,
    })
  },
  get_orders: function () {
    app.req("get_orders_ser.html", { "openid": app.data.openid, "status": that.data.currentTab, "page": that.data.pages[that.data.currentTab] }, function (res) {
      if (res.status == "success") {
        for (var i = 0; i < res.mess.length; i++) {
          if (that.data.currentTab == 0 && res.mess[i].order.status!=0) { that.data.orders.push(res.mess[i]) }
          if (that.data.currentTab == 1 && (res.mess[i].order.status == 1 || res.mess[i].order.status == 2)) { that.data.orderList1.push(res.mess[i]) }
          if (that.data.currentTab == 2 && res.mess[i].order.status == 3) { 
            that.data.orderList2.push(res.mess[i]) }
          if (that.data.currentTab == 3 && (res.mess[i].order.status == 4 || res.mess[i].order.status == 5)) { that.data.orderList3.push(res.mess[i]) }
        }
        // console.log(res)
        that.setData({ orders: that.data.orders });

        that.setData({ orderList1: that.data.orderList1 })
        that.setData({ orderList2: that.data.orderList2 })
        that.setData({ orderList3: that.data.orderList3 })

        that.data.pages[that.data.currentTab] = that.data.pages[that.data.currentTab] + 1;
        that.setData({ pages: that.data.pages })  //更新界面
        setTimeout(function () { that.setData({ shuaxin: true }) }, 1000)
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_orders()
          }
        })
      }
    })
  },
  tobottom: function () {
    if (that.data.shuaxin) {
      that.setData({ shuaxin: false })
      that.get_orders();
    }
  },


})